package com.fscp.seckill.mapper;

import com.fscp.commons.model.pojo.VoucherOrder;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

/**
 * 代金券订单 Mapper
 */
public interface VoucherOrderMapper {

    /**
     * 根据食客 ID 和秒杀 ID 查询代金券订单
     * 判断食客是否已抢过秒杀代金券，若抢过，不能再抢
     * 将原来的 param seckillId 改为 voucherId; 因为秒杀券活动信息存入 Redis 中，而 redis 无法自增处理原先的 seckillId，
     * 因此在表 t_voucher_order 中也无需维护该外键字段；而选用 fk_voucher_id 外键字段
     *
     * @param dinerId
     * @param voucherId
     * @return
     */
    @Select("select id, order_no, fk_voucher_id, fk_diner_id, qrcode, payment," +
            " status, fk_seckill_id, order_type, create_date, update_date, " +
            " is_valid from t_voucher_order where fk_diner_id = #{dinerId} " +
            " and fk_voucher_id = #{voucherId} and is_valid = 1 and status between 0 and 1  ")
    VoucherOrder findDinerOrder(@Param("dinerId") Integer dinerId,
                                 @Param("voucherId") Integer voucherId);

    // 新增代金券订单
    @Insert("insert into t_voucher_order (order_no, fk_voucher_id, fk_diner_id, " +
            " status, fk_seckill_id, order_type, create_date, update_date,  is_valid)" +
            " values (#{orderNo}, #{fkVoucherId}, #{fkDinerId}, #{status}, #{fkSeckillId}, " +
            " #{orderType}, now(), now(), 1)")
    int save(VoucherOrder voucherOrder);

}
